package com.hippy.erya.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.entity.DishDO;
import com.hippy.erya.entity.RawMaterialDictionaryDO;
import com.hippy.erya.entity.RawMaterialLossDO;
import com.hippy.erya.mapper.RawMaterialDictionaryMapper;
import com.hippy.erya.mapper.RawMaterialLossMapper;
import com.hippy.erya.query.BaseQueryWrapper;
import com.hippy.erya.sys.core.constant.cache.Cache;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class RawMaterialLossServiceImpl extends ServiceImpl<RawMaterialLossMapper, RawMaterialLossDO> {

    @Autowired
    private RawMaterialLossMapper rawMaterialLossMapper;

    @Autowired
    private RawMaterialDictionaryMapper rawMaterialDictionaryMapper;

    @Autowired
    private RawMaterialDictionaryServiceImpl rawMaterialDictionaryService;

    public Page<RawMaterialLossDO> queryByPage(long currentPage, int limit, String code) {
        Page<RawMaterialLossDO> page = new Page<>(currentPage, limit);
        QueryWrapper<RawMaterialLossDO> queryWrapper = BaseQueryWrapper.buildQuery();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.like("MATERIAL_CODE", code);
        }
        Page<RawMaterialLossDO> pageResult = rawMaterialLossMapper.selectPage(page, queryWrapper);
        Map<String, String> map = rawMaterialDictionaryService.codeToName(null);
        pageResult.getRecords().forEach(rawMaterialLossDO -> {
            BaseQueryWrapper.outputMessage(rawMaterialLossDO);
            String materialCode = rawMaterialLossDO.getMaterialCode();
            rawMaterialLossDO.setMaterialName(map.get(materialCode));
        });
        return pageResult;
    }

    public Map<String, RawMaterialLossDO> queryAllCode(String userCode) {
        QueryWrapper<RawMaterialLossDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USABLE_FLAG", Cache.USABLE);
        queryWrapper.orderByDesc("id");
        if (userCode == null) {
            userCode = String.valueOf(LoginContextHolder.getContext().getUser().getId());
        }
        queryWrapper.eq("USER_CODE", userCode);
        List<RawMaterialLossDO> rawMaterialLossDOS = rawMaterialLossMapper.selectList(queryWrapper);
        Map<String, RawMaterialLossDO> map = new HashMap<>();
        IterableUtils.forEach(rawMaterialLossDOS, rawMaterialLossDO -> {
            map.put(rawMaterialLossDO.getMaterialCode(), rawMaterialLossDO);
        });
        return map;
    }


    public List<RawMaterialLossDO> queryByRawMaterialCode(String materialCode) {
        QueryWrapper<RawMaterialLossDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.eq("MATERIAL_CODE", materialCode);
        return rawMaterialLossMapper.selectList(queryWrapper);
    }

    public RawMaterialDictionaryDO queryNameByRawMaterialCode(String materialCode) {
        QueryWrapper<RawMaterialDictionaryDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.eq("CODE", materialCode);
        return rawMaterialDictionaryMapper.selectOne(queryWrapper);
    }
}
